﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using WoodAdmin.Common.Attribute;
using WoodAdmin.Common.Helper;
using System.Linq;

namespace WoodAdmin.Common.Filter
{
    public class CheckLoginFilter : ActionFilterAttribute
    {
        /// <summary>
        /// 统一登陆验证
        /// </summary>
        /// <param name="context"></param>
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            //判断是否需要跳过校验
            bool isSkipCheckLogin = context.ActionDescriptor.EndpointMetadata.Any(a => a.GetType() == typeof(SkipCheckLogin));

            //1.判断 如果没有登陆并且 需要验证登陆的，则跳转到登陆页面 
            if(GlobalHelper.CurrentUser()==null && !isSkipCheckLogin ){
                 context.Result = new ViewResult() { ViewName = "/Views/Login/Login.cshtml" };
            }
        }
    }
}
